package xyz.thoughtset.viewer.executor.blocks.executor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import xyz.thoughtset.viewer.executor.blocks.tool.FunctionCallBlockToolCallbackProvider;
import xyz.thoughtset.viewer.modules.step.entity.BlockTypeEnum;
import xyz.thoughtset.viewer.modules.step.entity.FunctionCallBody;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockBodyEle;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockInfo;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
@Component
public class FunctionCallBlockExecutor extends AIChatExecutor<FunctionCallBody> {
    @Autowired
    private FunctionCallBlockToolCallbackProvider provider;

    @Override
    BlockTypeEnum getSupportType() {
        return BlockTypeEnum.FUNCTION_CALL;
    }

    @Override
    protected void loadToolCallbacks(BlockInfo block,FunctionCallBody body, ChatClient.Builder builder) {
        List<BlockBodyEle> funs = body.getBodyEles();
        if (!ObjectUtils.isEmpty(funs)){
            Map<String,BlockBodyEle> funParamMap = funs.parallelStream().collect(Collectors.toMap(
                    BlockBodyEle::getEleId, Function.identity()
            ));
            builder.defaultToolCallbacks(provider.getToolCallbacks(block.getId(),funParamMap));
        }
    }


}
